package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;



public class BibliotecaApp {

    public Library library;
    public static String result;
    public String endResult;
    public Customer customer;
    private static InputStream inputStream;

    public void setInputStream(InputStream inputStream) {this.inputStream = inputStream;}

    public BibliotecaApp(){

        library = Library.getObject();

    }

    public static void main(String[] args) {

        BibliotecaApp app = new BibliotecaApp();
        String option="";
        app.setInputStream(System.in);
        app.setAvailableBooks();
        app.setCustomers();
        app.setMovies();
        app.showLoginScreen();

    }

    public void setCustomers(){
        library.setCustomer(new Customer("User1","12345","user@mail.com","AAA-1234","*****"));
    }

    public void setAvailableBooks(){
        library.setAvailableBooks(new Book("Book1","A1","Author1","1.0.0","Publisher1"));
        library.setAvailableBooks(new Book("Book2","A2","Author2","1.0.0","Publisher2"));
    }

    public void setMovies(){

        library.setMovies(new Movie("movie1",1960,"director1",7,"M1"));
        library.setMovies(new Movie("movie2",1990,"director2",8,"M2"));

    }



    public void showLoginScreen(){

        String libraryNumber,password;
        System.out.println("~~~~~~~~@********WELCOME TO BIBLIOTECA********@~~~~~~~~");
        System.out.println("Enter Library Number:");
        libraryNumber = getCustomerInput();
        System.out.println("Enter Password:");
        password = getCustomerInput();
        validateCustomer(libraryNumber,password,false);

    }

    void validateCustomer(String libraryNumber,String password,boolean test){

        List<Customer> customers = library.getCustomers();
        boolean valid = false;

        for(Customer customer:customers){

            if(customer.getLibraryNumber().equals(libraryNumber) && customer.getPassword().equals(password)){
                valid = true;
               this.customer = customer;
            }
        }

        if(valid){
           result = "Valid customer";
            if(!test)
            showMenu("");
        }
        else {
            result = "The libraryNumber or password is wrong";
            System.out.println(result);
            if(!test)
            showLoginScreen();
        }
    }

    public void showMenu(String option){

        System.out.println("............................................");
        System.out.println("             Main Menu           ");
        System.out.println("List Books[L]");
        System.out.println("Return Book[R]");
        System.out.println("List Movies[M]");
        System.out.println("View Profile[P]");
        System.out.println("Quit[Q]");
        System.out.println("To select menu type the letter in square braces[]:");

        if(option.equals("")){
        option = getCustomerInput();}
        if(option == null){return;}
        if(option.equals("L")){
           showListOfBooks();
        }
        else if(option.equals("R")){
          library.returnBook("");
        }
        else if(option.equals("M")){
           showListOfMovies();

        }
        else if(option.equals("P")){
            customer.showProfile();
        }
        else if(option.equals("Q")){
            endResult = "Thanks for using biblioteca" ;
            System.out.println(endResult);

        }
        else{
           result = "Please enter a valid option";
            System.out.println(result);
           showMenu("");

        }
    }





    public void showCheckedOutBooks(){


        List<Book> checkedOutBooks = library.getCheckedOutBooks();
        if(checkedOutBooks.isEmpty()){
            System.out.println("There are no books CheckedOut");
            showMenu("");
        }
        for(Book book : checkedOutBooks) {
            System.out.println(book.getName() + "-" + book.getId());
        }
        System.out.println("Enter the Book ID to return book:");
    }

    public String getCustomerInput(){
        String option="";
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
        try{
            option = br.readLine();
               }
        catch(IOException e){
            System.out.println("ErrorStackTrace:" + e.getStackTrace());
        }

        return option;
    }

    public void showListOfMovies(){

        String option="";
        List<Movie> movies = library.getMovies();

        if(movies.isEmpty()){
            System.out.println("Sorry there are no movies available");
            showMenu("");
        }

        for(Movie movie:movies){
            System.out.print("Movie name:" + movie.getName() + " -MovieID:" + movie.getId() + " -Year:" + movie.getYear());
            System.out.println(" -Director:" + movie.getDirector() + " -Rating:" + movie.getRating());
        }

        System.out.println("Enter the movie name to checkout or [B] to go back to main menu");
        option =getCustomerInput();
        if(option.equals("B")){
           showMenu("");
        }
        else {
           library.checkoutMovie(option,movies);
        }

    }






    public void showListOfBooks(){

        String option = "";
        List<Book> availableBooks =library.getAvailableBooks();

        if (availableBooks.isEmpty()){
            System.out.println("Sorry there are no books available currently");
           showMenu("");
        }

        for(Book book : availableBooks) {
            System.out.println(book.getName() + "-" + book.getId());
        }

        System.out.println("Enter the Book ID to checkout or [B] to go back to main menu :");
        option = getCustomerInput();
        if(option.equals("B")){showMenu("");}
        else {
       library.checkoutBook(option,availableBooks);}
    }



    public void printValidity(boolean valid, String msg){

        if(valid){
            result=msg;
            System.out.println(msg);
            showMenu("");

        }
        else {
            result = "Please Enter a valid option";
            System.out.println(result);
            showMenu("");

        }
    }
}
